//
//  SHHomeCell.swift
//  SHWeibo
//
//  Created by jingming wang on 16/7/28.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit
import SnapKit

// cell中定义的间距
let homeCellMargin: CGFloat = 10
class SHHomeCell: UITableViewCell {
    
    // 记录底部视图的顶部约束
    var toolBarBottomConstraint:Constraint?
    // 数据模型
    var userInfoViewModel: SHUserInfoViewModel? {
        didSet {
            // 给原创微博赋值
            originalSectionView.userInfoViewModel = userInfoViewModel
            // 给底部视图赋值
            toolBarView.userInfoViewModel = userInfoViewModel
            
            /*
            - 首先要先卸载底部视图的顶部约束
            - 如果retweeted_status == nil 代表没有转发微博
            - 底部视图的top ==  原创微博的bottom
            - 隐藏转发微博
            - 反之 就是有转发微博
            - 给转发微博的ViewModel 赋值
            - 底部视图的top ==  转发微博的bottom
            - 显示转发微博
            */
            self.toolBarBottomConstraint?.uninstall()
            // 没有转发微博
            if ((userInfoViewModel?.data?.retweeted_status) == nil) {
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarBottomConstraint = make.top.equalTo(originalSectionView.snp_bottom).constraint
                })
                // 隐藏
                retweetSectionView.hidden = true
                
            }else {
                // 给转发微博赋值
                retweetSectionView.userInfoViewModel = userInfoViewModel
                // 有转发微博
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarBottomConstraint = make.top.equalTo(retweetSectionView.snp_bottom).constraint
                })
                // 显示
                retweetSectionView.hidden = false
            }

        }

    }
    
    // MARK: - 懒加载控件
    // 原创微博
    private lazy var originalSectionView: SHOriginalSectionView = SHOriginalSectionView()
    // 转发微博
    private lazy var retweetSectionView: SHRetweetSectionView = SHRetweetSectionView()
    // 底部视图
    private lazy var toolBarView: SHToolBarView = SHToolBarView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        backgroundColor = RGB(237, green: 237, blue: 237)
        contentView.addSubview(originalSectionView)
        contentView.addSubview(retweetSectionView)
        contentView.addSubview(toolBarView)
        
        originalSectionView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
            // 此约束已经在控件中约束了
            // make.height.equalTo(50)
        }
        
        retweetSectionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalSectionView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
            // 此约束已经在控件中约束了
            // make.height.equalTo(50)
        }
        
        toolBarView.snp_makeConstraints { (make) -> Void in
            self.toolBarBottomConstraint = make.top.equalTo(retweetSectionView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        // 约束contentView
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(toolBarView)
        }
    }

}
